package com.rocky.server.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.apache.log4j.Logger;
import org.json.JSONArray;

import com.rocky.server.ChatServer;
import com.rocky.server.Global;
import com.rocky.server.database.DBAdapter;
import com.rocky.server.model.User;
import com.rocky.server.timer.MetPointTimer;

public class InitServlet extends HttpServlet {
	private static Logger logger = Logger.getLogger(InitServlet.class);

	private static final long serialVersionUID = 1L;

	@Override
	public void init() throws ServletException {
		DBAdapter db = new DBAdapter();
		initAllUser(db);
		initAllFriend(db);
		db.closeConnect();
		new Thread(new Runnable() {
			public void run() {
				try {
					ChatServer cs = new ChatServer();
					cs.service();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}).start();
		
		new Thread(new MetPointTimer()).start();
		logger.debug("init done.");
	}

	public void initAllUser(DBAdapter db) {
		try {
			db.rs = db.stmt.executeQuery("select * from user");
			while (db.rs.next()) {
				User user = new User();
				user.setUserid(db.rs.getInt("userid"));
				user.setUsername(db.rs.getString("username"));
				user.setPassword(db.rs.getString("password"));
				user.setNickname(db.rs.getString("nickname"));
				user.setBirthday(db.rs.getDate("birthday"));
				user.setSex(db.rs.getInt("sex"));
				user.setMood(db.rs.getString("mood"));
				user.setPhoto(new JSONArray(db.rs.getString("photo")));
				user.setLocation_x(db.rs.getDouble("location_x"));
				user.setLocation_y(db.rs.getDouble("location_y"));
				String location_key = (db.rs.getString("location_key") == null ? "" : db.rs.getString("location_key"));
				user.setLocation_key(location_key);
				Global.userList.put(user.getUsername(), user);
				if (!location_key.equals("")) {
					if (Global.GeoUserList.get(location_key) != null) {
						Global.GeoUserList.get(location_key).put(
								user.getUserid(), user.getUsername());
					} else {
						HashMap<Integer, String> newHashMap = new HashMap<Integer, String>();
						newHashMap.put(user.getUserid(), user.getUsername());
						Global.GeoUserList.put(location_key, newHashMap);
					}
				}
			}
		} catch (Exception e) {
			logger.error(e.toString());
		}
	}

	public void initAllFriend(DBAdapter db) {
		try {
			db.rs = db.stmt
					.executeQuery("select usera.username as username,userb.username as friendname from friend,user usera,user userb where friend.userid = usera.userid and friend.friendid = userb.userid");
			while (db.rs.next()) {
				String username = db.rs.getString("username");
				String firendname = db.rs.getString("friendname");
				Global.userList.get(username).getFriendList().add(firendname);
			}
		} catch (SQLException e) {
			logger.error(e.toString());
		}
	}

}
